<?php
/**
 * 数据源操作类
 *
 * @author river_he@eueang.com
 */
class Kit_Db_Adapter {

	/**
	 * 设置参数
	 *
	 * @var array
	 */
	private $_config;

	/**
	 * 连接源
	 *
	 * @var $_link
	 */
	private $_link = null;

	/**
	 * construct
	 *
	 * @param array $config = array(
	 * 	'host'=>'',
	 * 	'username'=>'',
	 * 	'password'=>'',
	 * 	'dbname'=>'',
	 * 	'charset'=>'utf8'|'gbk'
	 * )
	 * @return void
	 */
	public function __construct(array $config){
		$this->_config = $config;
	}

	/**
	 * 获取连接源
	 *
	 * @return Resource
	 * @throws Kit_Db_Exception
	 */
	private function getLink(){
		if($this->_link === null){
			$link = mysql_connect($this->_config['host'], $this->_config['username'], $this->_config['password']);
			if($link){
				$this->_link = $link;
				if(isset($this->_config['dbname'])){
					mysql_select_db($this->_config['dbname'], $this->_link);
				}
				mysql_query("SET NAMES'utf8'", $this->_link);
			}else{
				throw new Kit_Db_Exception(mysql_error(), mysql_errno());
			}
		}
		return $this->_link;
	}

	/**
	 * 数据更新操作
	 *
	 * @param string $sql
	 * @return int
	 * @throws Kit_Db_Exception
	 */
	public function queryUpdate($sql){
		$return = 0;
		if($this->pretreatSql($sql)){
			$result = mysql_query($sql, $this->getLink());
			if($result){
				if(strtolower(substr($sql, 0, 6)) != 'insert'){
					$return = mysql_affected_rows($this->getLink());
				}else{
					$return = mysql_insert_id($this->getLink());
				}
			}else{
				throw new Kit_Db_Exception($sql.':'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
			}
		}
		return $return;
	}

	/**
	 * 获取单条记录操作
	 *
	 * @param string $sql
	 * @return Array
	 * @throws Kit_Db_Exception
	 */
	public function queryRow($sql){
		$return = array();
		if($this->pretreatSql($sql)){
			$result = mysql_query($sql, $this->getLink());
			if($result){
				$return = mysql_fetch_assoc($result);
				mysql_free_result($result);
			}else{
				throw new Kit_Db_Exception($sql.':'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
			}
		}
		return $return;
	}
	
	/**
	 * 获取条件记录总数
	 *
	 * @param string $sql
	 * @return int
	 * @throws Kit_Db_Exception
	 */
	public function queryTotal($sql){
		$return = 0;
		if($this->pretreatSql($sql)){
			$result = mysql_query($sql, $this->getLink());
			if($result){
				$row = mysql_fetch_row($result);
				$return += intval($row[0]);
				mysql_free_result($result);
			}else{
				throw new Kit_Db_Exception($sql.':'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
			}
		}
		return $return;
	}

	/**
	 * 获取条件记录集合
	 *
	 * @param string $query
	 * @return Array
	 * @throws Kit_Db_Exception
	 */
	public function queryAll($query){
		$return =array();
		if($this->pretreatSql($query)){
			$result = mysql_query($query, $this->getLink());
			if($result){
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
					array_push($return, $row);
				}
				mysql_free_result($result);
			}else{
				throw new Kit_Db_Exception($query.':'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
			}
		}
		return $return;
	}

	/**
	 * 开始事务处理
	 *
	 * @return void
	 * @throws Kit_Db_Exception
	 */
	public function startTrans(){
		$result = mysql_query('SET AUTOCOMMIT=0', $this->getLink());
		if(!$result){
			throw new Kit_Db_Exception('SET AUTOCOMMIT=0:'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
		}
	}
	
	/**
	 * sql安全处理
	 * 
	 * @param $sql
	 * @return boolean
	 */
	private function pretreatSql($sql){
		$fp = fopen('querylog.txt', 'a');
		fwrite($fp, $sql." \r\n");
		fclose($fp);
		/**
		 * @todo for test
		 */
		return true;
		$pre = strtolower(substr($sql, 0, 6));
		if(in_array($pre, array('delete', 'insert', 'select', 'update'))){
			return true;
		}
		return false;
	}

	/**
	 * 提交事务
	 *
	 * @return void
	 * @throws Kit_Db_Exception
	 */
	public function commitTrans(){
		$result = mysql_query('COMMIT', $this->getLink());
		if(empty($result)){
			throw new Kit_Db_Exception('COMMIT'.mysql_error($this->getLink()), mysql_errno($this->getLink()));
		}
	}

	/**
	 * destruct
	 *
	 * @return void
	 */
	public function __destruct(){
		if($this->_link !== null){
			mysql_close($this->_link);
		}
	}
}
//end